global !p
import vim
def math():
	return vim.funcs.luaeval("IsInTypstMath()")
endglobal

priority 100
snippet ,a "bold" wA
*$1*`!p
if t[2] and t[2][0] not in [',', '.', '?', '-', ' ', '，', '、', '；', '。']:
	snip.rv = ' '
else:
	snip.rv = ''
`${2:<<>>}
endsnippet
snippet ,i "italics" wA
_$1_`!p
if t[2] and t[2][0] not in [',', '.', '?', '-', ' ', '，', '、', '；', '。']:
	snip.rv = ' '
else:
	snip.rv = ''
`${2:<<>>}
endsnippet
snippet ,c "inline raw text" wA
#$0
endsnippet
snippet ,` "code blocks" bA
\`\`\`$0
<<>>
\`\`\`

<<>>
endsnippet
snippet ,l "link" wA
#link($1)[<<>>]`!p
if t[2] and t[2][0] not in [',', '.', '?', '-', ' ', '，', '、', '；', '。']:
	snip.rv = ' '
else:
	snip.rv = ''
`${2}
endsnippet
snippet "[,，]1" "Section" brA
= $1
endsnippet

snippet "[,，]2" "Sub Section" brA
== $1
endsnippet

snippet "[,，]3" "SubSub Section" brA
=== $1
endsnippet

snippet "[,，]4" "Paragraph" brA
==== $1
endsnippet

snippet "[,，]5" "Paragraph" brA
===== $1
endsnippet

snippet "[,，]6" "Sub Paragraph" brA
====== $1
endsnippet
snippet ,d "double quote" wA
"$0"
endsnippet
snippet ,s "single quote" wA
'$0'
endsnippet
snippet ,, "underline" iA
_$0
endsnippet
snippet ,p "image" wA
#image("$0")<<>>
endsnippet
snippet fig "figure" b
#figure(
image("$1")
caption: [<<>>]
) <<>>
endsnippet
snippet "([a-zA-Z0-9_])\[" "array.at()" ir
`!p snip.rv = match.group(1)`.at($0)<<>>
endsnippet

priority 200
# math snippets begin
snippet $$ "inline math" iA
$$1$`!p
if t[2] and t[2][0] not in [',', '.', '?', '-', ' ', '，', '、', '；', '。']:
	snip.rv = ' '
else:
	snip.rv = ''
`${2:<<>>}
endsnippet
snippet ￥￥ "inline math" iA
$$1$`!p
if t[2] and t[2][0] not in [',', '.', '?', '-', ' ', '，', '、', '；', '。']:
	snip.rv = ' '
else:
	snip.rv = ''
`${2:<<>>}
endsnippet
snippet mm "inline math" wA
$$1$ ${2:<<>>}
endsnippet
snippet nn "block math" bA
$ $0 $
<<>>
endsnippet
context "math()"
snippet .. "superscript" iA
^($0)<<>>
endsnippet
context "math()"
snippet ,, "subscript" iA
_($0)<<>>
endsnippet
context "math()"
snippet ,. "suber- and sub-script" iA
_($0)^(<<>>)<<>>
endsnippet
# context "math()"
# snippet lrp "( )" iA
# ($0)<<>>
# endsnippet
# context "math()"
# snippet lrb "[ ]" iA
# [$0]<<>>
# endsnippet
# context "math()"
# snippet lrc "{ }" iA
# {$0}<<>>
# endsnippet
# context "math()"
# snippet lra "< >" iA
# <$0><<>>
# endsnippet
# context "math()"
# snippet lr| "| |" iA
# |$0|<<>>
# endsnippet
context "math()"
snippet rt "sqrt()" wA
sqrt($0)<<>>
endsnippet
# math snippets end
